/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | Unsupported Contributions for OpenFOAM 
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2008-2010 OpenCFD Ltd.
     \\/     M anipulation  |
-------------------------------------------------------------------------------
<2010-06-10> <Guilherme A. L. Silva>: 
    added myalphatWallFunctionFvPatchScalarField function
    with new momentum-heat transfer analogy over rough surfaces developed and 
    validated by Silva, Arima, Branco and Pimenta (2010).
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "myalphatWallFunctionFvPatchScalarField.H"
#include "RASModel.H"
#include "fvPatchFieldMapper.H"
#include "volFields.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{
namespace compressible
{
namespace RASModels
{

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

myalphatWallFunctionFvPatchScalarField::
myalphatWallFunctionFvPatchScalarField
(
    const fvPatch& p,
    const DimensionedField<scalar, volMesh>& iF
)
:
    fixedValueFvPatchScalarField(p, iF),
    mutName_("mut"),
    Prt_(0.89),
    Ks_(0.0)
{}


myalphatWallFunctionFvPatchScalarField::
myalphatWallFunctionFvPatchScalarField
(
    const myalphatWallFunctionFvPatchScalarField& ptf,
    const fvPatch& p,
    const DimensionedField<scalar, volMesh>& iF,
    const fvPatchFieldMapper& mapper
)
:
    fixedValueFvPatchScalarField(ptf, p, iF, mapper),
    mutName_(ptf.mutName_),
    Prt_(ptf.Prt_),
    Ks_(ptf.Ks_)
{}


myalphatWallFunctionFvPatchScalarField::
myalphatWallFunctionFvPatchScalarField
(
    const fvPatch& p,
    const DimensionedField<scalar, volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchScalarField(p, iF, dict),
    mutName_(dict.lookupOrDefault<word>("mut", "mut")),
    Prt_(dict.lookupOrDefault<scalar>("Prt", 0.89)),
    Ks_(dict.lookupOrDefault<scalar>("Ks", 0.0))
{}


myalphatWallFunctionFvPatchScalarField::
myalphatWallFunctionFvPatchScalarField
(
    const myalphatWallFunctionFvPatchScalarField& awfpsf
)
:
    fixedValueFvPatchScalarField(awfpsf),
    mutName_(awfpsf.mutName_),
    Prt_(awfpsf.Prt_),
    Ks_(awfpsf.Ks_)
{}


myalphatWallFunctionFvPatchScalarField::
myalphatWallFunctionFvPatchScalarField
(
    const myalphatWallFunctionFvPatchScalarField& awfpsf,
    const DimensionedField<scalar, volMesh>& iF
)
:
    fixedValueFvPatchScalarField(awfpsf, iF),
    mutName_(awfpsf.mutName_),
    Prt_(awfpsf.Prt_),
    Ks_(awfpsf.Ks_)
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

void myalphatWallFunctionFvPatchScalarField::updateCoeffs()
{
    if (updated())
    {
        return;
    }

    const label patchI = patch().index();

    const RASModel& rasModel = db().lookupObject<RASModel>("RASProperties");

    //flow properties
    const scalarField& rhow = rasModel.rho().boundaryField()[patchI];
    const scalarField& muw = rasModel.mu().boundaryField()[patchI];
    const scalarField& alphaw = rasModel.alpha().boundaryField()[patchI];
    const scalarField& mutw =
        patch().lookupPatchField<volScalarField, scalar>(mutName_);

    const fvPatchVectorField& Up = rasModel.U().boundaryField()[patchI];  
    const scalarField magUp = mag(Up.patchInternalField() - Up);
    scalarField magFaceGradU = mag(Up.snGrad());

    const scalar smallNum = 1e-14;

    tmp<scalarField> talpha(new scalarField(*this));
    scalarField& alphaTurb = talpha();
    scalar aFun = 0.5;

    forAll(mutw, faceI)
    {
        scalar pr = muw[faceI]/alphaw[faceI];
	scalar tau = (muw[faceI]+mutw[faceI])*magFaceGradU[faceI];
        scalar uTau = sqrt(max(tau/rhow[faceI],smallNum));
        scalar KsPlus = uTau*Ks_/(muw[faceI]/rhow[faceI]);
	scalar sqrtCfOver2 = uTau/max(magUp[faceI],smallNum);
	scalar Stk = 1.42*pow(max(KsPlus,smallNum),-0.45)*
					pow(max(pr,smallNum),-0.8);
	alphaTurb[faceI] = 1/Prt_;

	if (KsPlus > 5)
	{
	   scalar heatAnalogy = 1/(Prt_+sqrtCfOver2/max(Stk,smallNum))*
			1/(Prt_*aFun);
	   alphaTurb[faceI] = heatAnalogy;
	}
    }

    operator==(mutw*alphaTurb);
    fixedValueFvPatchScalarField::updateCoeffs();
}

void myalphatWallFunctionFvPatchScalarField::write(Ostream& os) const
{
    fvPatchField<scalar>::write(os);
    writeEntryIfDifferent<word>(os, "mut", "mut", mutName_);
    os.writeKeyword("Prt") << Prt_ << token::END_STATEMENT << nl;
    os.writeKeyword("Ks") << Ks_ << token::END_STATEMENT << nl;
    writeEntry("value", os);
}


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

makePatchTypeField(fvPatchScalarField, myalphatWallFunctionFvPatchScalarField);

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace RASModels
} // End namespace compressible
} // End namespace Foam

// ************************************************************************* //
